import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)
const routes = [
	{
		path: "/login",
		name: 'login',
		meta: {
			title: "登录"
		},
		component: () => import('../views/login.vue')
	},
	{
		path: "/",
		meta: {
			title: "主页"
		},
		component: () => import('../views/Zhuye.vue'),
		children: [{
				path: "",
				redirect: "newsList",
				meta: {
					title: "重定向",
					hidden: true
				}
			},
			{
				path: "newsList",
				name: "newsList",
				meta: {
					title: "新闻列表",
					icon: require('@/assets/icon/newsList_icon.png'),
					icon_sl: require('@/assets/icon/newsList_icon_sl.png'),
				},
				component: () => import( /* webpackChunkName: "about" */ '../views/newsList.vue')
			},
			{
				path: "newsType",
				name: "newsType",
				meta: {
					title: "新闻分类",
					icon: require('@/assets/icon/newsType_icon.png'),
					icon_sl: require('@/assets/icon/newsType_icon_sl.png'),
				},
				component: () => import( /* webpackChunkName: "about" */ '../views/newsType.vue')
			},
			{
				path: "users",
				name: "users",
				meta: {
					title: "用户管理",
					icon: require('@/assets/icon/users_icon.png'),
					icon_sl: require('@/assets/icon/users_icon_sl.png'),
				},
				component: () => import( /* webpackChunkName: "about" */ '../views/users.vue')
			},
			{
				path: "swiperMgr",
				name: "swiperMgr",
				meta: {
					title: "轮播图管理",
					icon: require('@/assets/icon/swiperMgr_icon.png'),
					icon_sl: require('@/assets/icon/swiperMgr_icon_sl.png'),
				},
				component: () => import( /* webpackChunkName: "about" */ '../views/swiperMgr.vue')
			},
		]
	},
	{
		path: "/register",
		name: 'register',
		meta: {
			title: "注册"
		},
		component: () => import('../views/register.vue')
	},

]

const router = new VueRouter({
	routes,
	// 修改路径方式history/hash/
	mode:"history"
})

router.beforeEach((to, from, next) => {
	// 修改页面的title
	window.document.title = 'rain_' + to.meta.title;
	// to 将要返回的路径，from 从哪个路径跳过来，next 一个函数 表示放行
	if (to.path === '/login' || to.path === '/register') return next();
	// 获取token
	const tokenStr = localStorage.getItem('token');
	console.log(95, tokenStr)
	if (!tokenStr) return next('/login');
	next();
})

export default router
